package com.gitee.swsk33.catserviceprovider.service.impl;

import com.gitee.swsk33.catcommon.ClassExamine;
import com.gitee.swsk33.catentity.dataobject.Cat;
import com.gitee.swsk33.catentity.model.Result;
import com.gitee.swsk33.catserviceprovider.dao.CatDAO;
import com.gitee.swsk33.catserviceprovider.service.CatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class CatServiceImpl implements CatService {

	@Autowired
	private CatDAO catDAO;

	@Override
	public Result add(Cat cat) {
		Result result = new Result();
		if (catDAO.add(cat) < 1) {
			result.setResultFailed("添加猫失败！");
			return result;
		}
		result.setResultSuccess("添加猫成功！");
		return result;
	}

	@Override
	public Result delete(int id) {
		Result result = new Result();
		if (catDAO.delete(id) < 1) {
			result.setResultFailed("删除猫失败！");
			return result;
		}
		result.setResultSuccess("删除猫成功！");
		return result;
	}

	@Override
	public Result update(Cat cat) throws Exception {
		Result result = new Result();
		Cat getCat = catDAO.getById(cat.getId());
		if (getCat == null) {
			result.setResultFailed("找不到这个猫！");
			return result;
		}
		ClassExamine.objectOverlap(cat, getCat);
		if (catDAO.update(cat) < 1) {
			result.setResultFailed("更新失败！");
			return result;
		}
		result.setResultSuccess("更新成功！");
		return result;
	}

	@Override
	public Result<Cat> getById(int id) {
		Result<Cat> result = new Result<>();
		Cat getCat = catDAO.getById(id);
		if (getCat == null) {
			result.setResultFailed("找不到这个猫！");
			return result;
		}
		result.setResultSuccess("查找成功！", getCat);
		return result;
	}

}